package com.apac.dao;

import java.util.List;

import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

import com.apac.bom.AbstractBOM;
import com.apac.bom.Employee;
import com.apac.bom.EmployeeAccountRequirement;

public class EmployeeDAO extends AbstractDAO {

	@Override
	public AbstractBOM findByKey(String id) {
		// TODO Auto-generated method stub
		Employee employee = null;

		employee = em.find(Employee.class, id);
		return employee;
	}

	@Override
	public List<AbstractBOM> findAll() {
		// TODO Auto-generated method stub
		List<AbstractBOM> employees = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
		Root<Employee> root = cq.from(Employee.class);
		cq.select(root);
		TypedQuery tq = em.createQuery(cq);
		employees = tq.getResultList();

		return employees;
	}
	
	public byte[] findPhotoById(String id){
		byte[] photo = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createTupleQuery();
		Root<Employee> root = cq.from(Employee.class);
		ParameterExpression<String> nameParameter = cb.parameter(String.class,
				"empid");
		cq.multiselect(root.get("empPhoto")).where(cb.equal(root.<String>get("empIdNo"),nameParameter));
		TypedQuery<Tuple> tq = em.createQuery(cq);
		tq.setParameter("empid", id);
		List<Tuple> tupleResult = tq.getResultList();
		for (Tuple t : tupleResult) {
		    photo = (byte []) t.get(0);
		}
		
		return photo;
	}
	
	@Override
	public boolean update(AbstractBOM entity) {
		// TODO Auto-generated method stub
		try {
//			Employee employee = (Employee) this.findByKey(((Employee) entity).getEmpIdNo());
			tx.begin();
//			Employee tempEmp = (Employee)entity;
//			
//			if(tempEmp.getEmpPhoto()==null){
//				tempEmp.setEmpPhoto(employee.getEmpPhoto());
//			}
			
			em.merge((Employee)entity);
//			employee = (Employee) entity;
//			employee.setEmpBday(((Employee) entity).getEmpBday());
//			employee.setEmpCelNo(((Employee) entity).getEmpCelNo());
//			employee.setEmpCntctAddr(((Employee) entity).getEmpCntctAddr());
//			employee.setEmpCntctNo(((Employee) entity).getEmpCntctNo());
//			employee.setEmpCntctPrsn(((Employee) entity).getEmpCntctPrsn());
//			employee.setEmpCntctRel(((Employee) entity).getEmpCntctRel());
//			employee.setEmpColl(((Employee) entity).getEmpColl());
//			employee.setEmpCtshp(((Employee) entity).getEmpCtshp());
//			employee.setEmpCvStat(((Employee) entity).getEmpCvStat());
//			employee.setEmpDateHired(((Employee) entity).getEmpDateHired());
//			employee.setEmpEadd(((Employee) entity).getEmpEadd());
//			employee.setEmpExpYrs(((Employee) entity).getEmpExpYrs());
//			employee.setEmpFirstName(((Employee) entity).getEmpFirstName());
//			employee.setEmpGndr(((Employee) entity).getEmpGndr());
//			employee.setEmpHdmfNo(((Employee) entity).getEmpHdmfNo());
//			employee.setEmpHlthCardNo(((Employee) entity).getEmpHlthCardNo());
//			employee.setEmpHlthCardPrem(((Employee) entity).getEmpHlthCardPrem());
//			employee.setEmpLandNo(((Employee) entity).getEmpLandNo());
//			employee.setEmpLastName(((Employee) entity).getEmpLastName());
//			employee.setEmpLicsn(((Employee) entity).getEmpLicsn());
//			employee.setEmployeeAccountRequirements(((Employee) entity).getEmployeeAccountRequirements());
//			employee.setEmployeeProjects(((Employee) entity).getEmployeeProjects());
//			employee.setEmploymentSalaryHistory(((Employee) entity).getEmploymentSalaryHistory());
//			employee.setEmpMidName(((Employee) entity).getEmpMidName());
//			employee.setEmpPhlHlthNo(((Employee) entity).getEmpPhlHlthNo());
//			employee.setEmpPhoto(((Employee) entity).getEmpPhoto());
//			employee.setEmpPresAddr(((Employee) entity).getEmpPresAddr());
//			employee.setEmpProvAddr(((Employee) entity).getEmpProvAddr());
//			employee.setEmpRmks(((Employee) entity).getEmpRmks());
//			employee.setEmpSkills(((Employee) entity).getEmpSkills());
//			employee.setEmpStatus(((Employee) entity).getEmpStatus());
//			employee.setEmpSssNo(((Employee) entity).getEmpSssNo());
//			employee.setEmpTin(((Employee) entity).getEmpTin());
//			employee.setHealthCardDependents(((Employee) entity).getHealthCardDependents());
//			employee.setUser(((Employee) entity).getUser());
//			em.flush();
//			em.clear();
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
		} finally {
			em.close();
		}
		return false;
	}

	@Override
	public boolean delete(AbstractBOM entity) {
		try {
			tx.begin();
			Employee employee = (Employee) this.findByKey(((Employee) entity)
					.getEmpIdNo());
			em.remove(employee);
			em.flush();
			em.clear();
			tx.commit();
			return true;
		} catch (Exception e) {
			tx.rollback();
		} finally {
			em.close();
		}
		return false;
	}

	public List<EmployeeAccountRequirement> retrieveAllRequirements(String id) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<EmployeeAccountRequirement> cq = cb
				.createQuery(EmployeeAccountRequirement.class);
		Root<EmployeeAccountRequirement> root = cq
				.from(EmployeeAccountRequirement.class);
		ParameterExpression<String> param = cb.parameter(String.class, "empid");
		cq.select(root).where(
				cb.equal(root.get("Employee").<String> get("empIdNo"), param));

		TypedQuery<EmployeeAccountRequirement> tq = em.createQuery(cq);
		tq.setParameter("empid", id);

		List<EmployeeAccountRequirement> requirements = tq.getResultList();
		return requirements;
	}
}
